package com.ajx.blog.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ajx.blog.entity.Blogusers;
import com.ajx.blog.entity.CourseInfo;
import com.ajx.blog.mapper.BlogusersMapper;
import com.ajx.blog.mapper.CourseInfoMapper;
import com.ajx.blog.service.api.CourseInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfo> implements CourseInfoService {

    @Autowired
    private CourseInfoMapper courseInfoMapper;

    @Autowired
    private BlogusersMapper blogusersMapper;

    /**
     * @param {CourseInfo} courseInfo 待添加课程
     * @return {boolean} 是否添加成功 true/false
     * 
     * @description: 添加一门课程信息
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean insertOneCourse(CourseInfo courseInfo) {
        return courseInfoMapper.insert(courseInfo) > 0;
    }

    /**
     * @param {CourseInfo} courseInfo 待更新课程
     * @return {boolean} 是否更新成功 true/false
     * 
     * @description: 更新一门课程信息
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean updateOneCourse(CourseInfo courseInfo) {
        return courseInfoMapper.updateById(courseInfo) > 0;
    }

    /**
     * @param {Integer} page 当前页码数
     * @return {PageInfo<CourseInfo>} 封装的分页信息对象
     * 
     * @description: 分页查询课程列表
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PageInfo<CourseInfo> selectCourseList(Integer page) {
        PageHelper.startPage(page, 2);
        QueryWrapper<CourseInfo> qw = new QueryWrapper<>();
        List<CourseInfo> courseInfoList = courseInfoMapper.selectList(qw);
        PageInfo<CourseInfo> pageInfo = new PageInfo<>(courseInfoList);
        return pageInfo;
    }

    /**
     * @param {Integer} pageNum 当前页码数
     * @param {Integer} pageSize 每页记录数量
     * @return {PageInfo<CourseInfo>} 封装的分页信息对象
     * 
     * @description: 分页查询课程列表
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PageInfo<CourseInfo> selectCourseList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<CourseInfo> qw = new QueryWrapper<>();
        List<CourseInfo> courseInfoList = courseInfoMapper.selectList(qw);
        PageInfo<CourseInfo> pageInfo = new PageInfo<>(courseInfoList);
        return pageInfo;
    }

    /**
     * @param {Integer} pageNum 当前页码数
     * @param {Integer} pageSize 每页记录数量
     * @param {String}  sortBy 排序字段
     * @param {String}  ordBy 正序/倒序
     * @return {PageInfo<CourseInfo>} 封装的分页信息对象
     * 
     * @description: 分页查询课程列表
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PageInfo<CourseInfo> selectCourseListByOrder(Integer pageNum, Integer pageSize, String sortBy,
            String ordBy) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<CourseInfo> qw = new QueryWrapper<>();
        if ("asc".equals(ordBy)) {
            qw.orderByAsc(sortBy);
        } else {
            qw.orderByDesc(sortBy);
        }
        List<CourseInfo> courseInfoList = courseInfoMapper.selectList(qw);
        PageInfo<CourseInfo> pageInfo = new PageInfo<>(courseInfoList);
        return pageInfo;
    }

    /**
     * @param {Integer} cid 课程id
     * @return {CourseInfo} 课程id对应的课程对象
     * 
     * @description: 根据课程id查询课程
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public CourseInfo selectCourseById(Integer cid) {
        return courseInfoMapper.selectById(cid);
    }

    /**
     * @param {Integer} courseId 课程编号
     * @param {Integer} page 当前页码数
     * @return {PageInfo<Blogusers>} 封装的分页数据集合
     * 
     * @description: 通过分页数据和当前页码数查询选了此门课程的用户集合
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PageInfo<Blogusers> selectUserListByCourseId(Integer courseId, Integer page) {
        QueryWrapper<Blogusers> blogusersQW = new QueryWrapper<>();
        blogusersQW.inSql("uid", "select userId from course_person where cid = " + courseId);
        PageHelper.startPage(page, 5);
        List<Blogusers> blogusersList = blogusersMapper.selectList(blogusersQW);
        PageInfo<Blogusers> pageInfo = new PageInfo<>(blogusersList);
        return pageInfo;
    }

    /**
     * @param {String} timeStr 时间范围 thisMonth代表本月
     * @return {Map<String,Integer>} 课程名与课程收益对应的map集合
     * 
     * @description: 查询课程的收益
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Cacheable(cacheNames = "courseEarningsMap", unless = "#result == null")
    @Override
    public Map<String, Integer> courseEarningsMap(String timeStr) {
        Map<String, Integer> returnMap = new HashMap<>();
        for (CourseInfo courseInfo : courseInfoMapper.courseEarningsMap(timeStr)) {
            returnMap.put(courseInfo.getTitle(), courseInfo.getMoney());
        }
        return returnMap;
    }

    /**
     * @param {*}
     * @return {Integer} 所有课程数量
     * 
     * @description: 查询所有课程数量
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Cacheable(cacheNames = "selectCourseCount", unless = "#result == null")
    @Override
    public Integer selectCourseCount() {
        QueryWrapper<CourseInfo> courseInfoQW = new QueryWrapper<>();
        return courseInfoMapper.selectCount(courseInfoQW);
    }

}
